package com.fengzheng.basic.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fengzheng.basic.common.config.response.ResultBean;
import com.fengzheng.basic.model.po.SysUserPo;
import com.fengzheng.basic.model.dto.SysUserDto;
import com.fengzheng.basic.model.vo.SysUserVo;
import com.fengzheng.basic.service.sys.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/sys/user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @PostMapping("/save")
    public ResultBean<Long> save(@RequestBody SysUserDto sysUserDto){
        SysUserPo sysUserPo = BeanUtil.copyProperties(sysUserDto,SysUserPo.class);
        sysUserPo.setPassword(new BCryptPasswordEncoder().encode(sysUserPo.getPassword()));

        if(sysUserService.saveOrUpdate(sysUserPo)){
            return new ResultBean(sysUserPo.getId());
        }else{
            return new ResultBean(false);
        }
    }

    @PostMapping("/delete/{id}")
    public ResultBean delete(@PathVariable Long id){
        return new ResultBean(sysUserService.removeById(id));
    }

    @GetMapping("/find")
    public SysUserVo find(String account){
        SysUserPo sysUserPo = sysUserService.getOne(Wrappers.<SysUserPo>lambdaQuery().eq(SysUserPo::getAccount,account));
        SysUserVo sysUserVo = BeanUtil.copyProperties(sysUserPo,SysUserVo.class);
        return sysUserVo;
    }

}
